package net.m510.autosurfer.smbc;

import java.net.URI;
import java.util.logging.Logger;

import net.m510.autosurfer.common.DocumentGetProcess;
import net.m510.autosurfer.common.exception.InvalidDocumentException;

import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class SmbcGoDetail extends DocumentGetProcess {
	private static final Logger log = Logger.getLogger(SmbcGoDetail.class.getName());
	
	private String accountNum;

	public SmbcGoDetail() {
		this(null,null,null);
	}
	
	public SmbcGoDetail(CloseableHttpClient client,HttpClientContext context, String accountNum) {
		super(client, context);
		super.log = log;
		this.accountNum = accountNum;
	}

	@Override
	protected URI getUri(Document doc) {
		// 口座の詳細ページへのURLを取得
		final String ID_KEY="top-02";
		Element top_e = doc.getElementById(ID_KEY);
		if ( top_e == null ) throw new InvalidDocumentException("id not found "+ID_KEY,doc);
		final String CLASS_KEY="dataTable";
		Elements e2 = top_e.getElementsByClass(CLASS_KEY);
		if ( e2.isEmpty() ) throw new InvalidDocumentException("class not found "+CLASS_KEY,doc);
		StringBuilder ptn = new StringBuilder("a[href]:matchesOwn(.*").append(accountNum).append(".*)");
		Elements anchors = e2.select(ptn.toString());
		if ( anchors.size() == 0 ) throw new IllegalArgumentException("account num pattern not found : "+ptn.toString());
		
		return URI.create(anchors.first().attr("href"));
	}
}
